package com.alpha.eceasy.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.alpha.eceasy.auth.UserLoginDTO;
import com.alpha.eceasy.core.R;
import com.alpha.eceasy.mall.api.dto.UpdatePasswordDTO;
import com.alpha.eceasy.mall.api.entity.MallCustomer;
import com.alpha.eceasy.mall.service.impl.MallCustomerServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/customer", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(description = "customer", name = "客户表")
public class MallCustomerController {
    @Resource
    private MallCustomerServiceImpl mallCustomerService;

    /**
     * 登录
     *
     * @param dto 用户详情
     * @return 成功与否
     */
    @Operation(description = "登录", summary = "登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody @Validated UserLoginDTO dto) {
        log.info("login: {}", dto);
        return R.ok(mallCustomerService.login(dto));
    }

    /**
     * 登出
     */
    @Operation(description = "登出", summary = "登出")
    @PostMapping("/logout")
    public Boolean logout() {
        return mallCustomerService.logout();
    }

    /**
     * 查看个人信息
     */
//    @PreAuthorize("hasRole('ROLE_USER')")
    @Operation(description = "查看个人信息", summary = "查看个人信息")
    @GetMapping("/info")
    public MallCustomer info() {
        return mallCustomerService.info();
    }

    /**
     * 修改个人基础信息
     *
     * @param sysUser 用户基础信息
     * @return 成功与否
     */
    @Operation(summary = "修改个人基础信息", description = "注意：此接口无法修改密码，如需修改密码请参阅/info/password")
    @PutMapping("/info")
    public boolean updateInfo(@RequestBody MallCustomer sysUser) {
        return mallCustomerService.updateInfo(sysUser);
    }

    /**
     * 修改个人密码
     *
     * @param dto 密码
     * @return 成功与否
     */
    @Operation(description = "修改个人密码", summary = "修改个人密码")
    @PutMapping("/info/password")
    public boolean updateInfoPassword(@RequestBody UpdatePasswordDTO dto) {
        return mallCustomerService.updateInfoPassword(dto);
    }

    /**
     * 用户注册
     *
     * @param entity 用户注册
     * @return 成功与否
     */
    @Operation(description = "用户注册", summary = "用户注册")
    @PostMapping("/register")
    public Boolean register(@RequestBody @Validated MallCustomer entity) {
        return mallCustomerService.save(entity);
    }

    /**
     * 分页查询客户详情
     *
     * @return 客户详情
     */
    @Operation(description = "分页查询", summary = "分页查询客户详情")
    @GetMapping("")
    public Page<MallCustomer> page(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam(value = "sellerGuid", required = false) String sellerGuid,
                                   @RequestParam(value = "isBind", required = false) Boolean isBind) {
        return mallCustomerService.page(Page.of(pageNum, pageSize), Wrappers.<MallCustomer>lambdaQuery()
                .eq(StrUtil.isNotBlank(sellerGuid), MallCustomer::getSellerGuid, sellerGuid)
                .isNull(Boolean.FALSE.equals(isBind), MallCustomer::getSellerGuid)
        );
    }

    /**
     * 根据guid查询客户详情
     *
     * @return 客户详情
     */
    @Operation(description = "根据guid查询客户详情", summary = "根据guid查询客户详情")
    @GetMapping("/{guid}")
    public MallCustomer getByGuid(@PathVariable("guid") String guid) {
        return mallCustomerService.getByGuid(guid);
    }

    /**
     * 新增一个客户详情
     *
     * @param entity 客户详情详情
     * @return 成功与否
     */
    @Operation(description = "新增一个客户详情", summary = "新增一个客户详情")
    @PostMapping("")
    public Boolean save(@RequestBody @Validated MallCustomer entity) {
        return mallCustomerService.save(entity);
    }

    /**
     * 根据guid修改一个客户详情
     *
     * @param entity 客户详情详情
     * @return 成功与否
     */
    @Operation(description = "根据guid修改一个客户详情", summary = "根据guid修改一个客户详情")
    @PutMapping("")
    public Boolean updateByGuid(@RequestBody MallCustomer entity) {
        return mallCustomerService.updateByGuid(entity);
    }

    /**
     * 根据guid删除一个客户详情
     *
     * @param guid guid
     * @return 成功与否
     */
    @Operation(description = "根据guid删除一个客户详情", summary = "根据guid删除一个客户详情")
    @DeleteMapping("/{guid}")
    public Boolean removeByGuid(@PathVariable("guid") String guid) {
        return mallCustomerService.removeByGuid(guid);
    }
}
